package com.unlcn.ils.kas.service.order.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.unlcn.ils.kas.dao.model.order.OrderOtd;
import com.unlcn.ils.kas.dao.model.order.bo.OnWayExceptionBo;
import com.unlcn.ils.kas.dao.model.order.bo.OrderSelectBo;
import com.unlcn.ils.kas.service.order.ExcelService;
import com.unlcn.ils.kas.service.order.OrderOtdService;
import com.unlcn.ils.kas.service.util.enums.order.OrderExceptionClassEnum;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class ExcelServiceImpl implements ExcelService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExcelServiceImpl.class);

    @Autowired
    private OrderOtdService orderOtdService;

    @Override
    public List<OrderOtd> export(OrderSelectBo orderSelectBo) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        EntityWrapper<OrderOtd> ew = new EntityWrapper<>();

        if (orderSelectBo != null) {
            // 系统订单号
            if (!StringUtils.isEmpty(orderSelectBo.getSysOrderNo())) {
                String[] orders = orderSelectBo.getSysOrderNo().split("\\s+");
                LOGGER.error("Sys_order_no length is ..." + orders.length);
                if (orders.length == 1) {
                    ew.like("sys_order_no", orders[0]);
                } else {
                    ew.in("sys_order_no", orders);
                }
            }
            // 客户订单号
            if (!StringUtils.isEmpty(orderSelectBo.getCustomerOrderNo())) {
                ew.like("customer_order_no", orderSelectBo.getCustomerOrderNo());
            }
            // 客户编号
            if (!StringUtils.isEmpty(orderSelectBo.getCustomerNo())) {
                ew.eq("customer_no", orderSelectBo.getCustomerNo());
            }
            // 客户名称
            if (!StringUtils.isEmpty(orderSelectBo.getCustomerName())) {
                ew.like("customer_name", orderSelectBo.getCustomerName());
            }
            try {
                // 订单起始日期
                if (!StringUtils.isEmpty(orderSelectBo.getStartOrderTime())
                        && !orderSelectBo.getStartOrderTime().equals("null")) {
                    ew.ge("order_create_time", orderSelectBo.getStartOrderTime());
                }
                // 订单截至日期
                if (!StringUtils.isEmpty(orderSelectBo.getEndOrderTime())
                        && !orderSelectBo.getEndOrderTime().equals("null")) {
                    ew.lt("order_create_time", orderSelectBo.getEndOrderTime());
                }
                // 起始打单时间
                if (!StringUtils.isEmpty(orderSelectBo.getStartPrintOrderTime())
                        && !orderSelectBo.getStartPrintOrderTime().equals("null")) {
                    ew.ge("printOrder_time", format.parse(orderSelectBo.getStartPrintOrderTime()));
                }
                if (!StringUtils.isEmpty(orderSelectBo.getEndPrintOrderTime())
                        && !orderSelectBo.getEndPrintOrderTime().equals("null")) {
                    ew.lt("printOrder_time", format.parse(orderSelectBo.getEndPrintOrderTime()));
                }
                // 起始回单时间
                if (!StringUtils.isEmpty(orderSelectBo.getStartReturnOrderTime())
                        && !orderSelectBo.getStartReturnOrderTime().equals("null")) {
                    ew.ge("returnOrder_time", format.parse(orderSelectBo.getStartReturnOrderTime()));
                }
                // 截至回单时间
                if (!StringUtils.isEmpty(orderSelectBo.getEndReturnOrderTime())
                        && !orderSelectBo.getEndReturnOrderTime().equals("null")) {
                    ew.lt("returnOrder_time", format.parse(orderSelectBo.getEndReturnOrderTime()));
                }
            } catch (Exception e) {
                LOGGER.error("ExcelServiceImpl.export ERROR Message : {} ", e);
            }
            // 车架号
            // 单个车架号用like 否则in
            if (!StringUtils.isEmpty(orderSelectBo.getVin())) {
                String[] vins = orderSelectBo.getVin().split("\\s+");
                if (vins.length == 1) {
                    ew.like("vin", vins[0]);
                } else {
                    ew.in("vin", vins);
                }
            }

            // 收车省
            if (!StringUtils.isEmpty(orderSelectBo.getdProvince())) {
                ew.eq("d_province", orderSelectBo.getdProvince());
            }
            // 收车市
            if (!StringUtils.isEmpty(orderSelectBo.getdCity())) {
                ew.like("d_city", orderSelectBo.getdCity());
            }
            // 订单状态
            if (!StringUtils.isEmpty(orderSelectBo.getOrderStatus())) {
                ew.eq("order_status", orderSelectBo.getOrderStatus());
            }
            // 运输方式
            if (!StringUtils.isEmpty(orderSelectBo.getTransportType())) {
                ew.eq("transport_type", orderSelectBo.getTransportType());
            }
            // 车牌号
            if (!StringUtils.isEmpty(orderSelectBo.getLicensePlate())) {
                ew.like("license_plate", orderSelectBo.getLicensePlate());
            }
            // 承运车队
            if (!StringUtils.isEmpty(orderSelectBo.getSupplier())) {
                ew.like("supplier", orderSelectBo.getSupplier());
            }
            // 经销商名称
            if (!StringUtils.isEmpty(orderSelectBo.getDealerName())) {
                ew.like("dealer_name", orderSelectBo.getDealerName());
            }
            // 经销商编码
            if (!StringUtils.isEmpty(orderSelectBo.getDealerNo())) {
                ew.eq("dealer_no", orderSelectBo.getDealerNo());
            }

        }

        List<OrderOtd> orderOtdList = orderOtdService.selectList(ew);

        return orderOtdList;
    }

    @Override
    public List<OrderOtd> getExceptionExcel(Integer customerNo, Integer classCode) {

        OnWayExceptionBo onWayExceptionBo = new OnWayExceptionBo();

        if (classCode == OrderExceptionClassEnum.CLASS_TWO.getCode()) {
            String format = "yyyy-MM-dd HH:mm:ss";
            DateTime dt = new DateTime();
            Integer year = dt.getYear();
            Integer month = dt.getMonthOfYear();
            int day = dt.getDayOfMonth();
            DateTime todayDateTime = new DateTime(year, month, day, 0, 0, 0);

            DateTime yesterdayStartDateTime = todayDateTime.minusDays(1);
            DateTime yesterdayEndDateTime = todayDateTime;

            DateTime beforeYesterdayStartDateTime = todayDateTime.minusDays(2);
            DateTime beforeYesterdayEndDateTime = todayDateTime.minusDays(1);

            String yesterdayStartDate = yesterdayStartDateTime.toString(format);
            String yesterdayEndDate = yesterdayEndDateTime.toString(format);
            onWayExceptionBo.setYesterdayStartDate(yesterdayStartDate);
            onWayExceptionBo.setYesterdayEndDate(yesterdayEndDate);

            String beforeYesterdayStartDate = beforeYesterdayStartDateTime.toString(format);
            String beforeYesterdayEndDate = beforeYesterdayEndDateTime.toString(format);
            onWayExceptionBo.setBeforeYesterdayStartDate(beforeYesterdayStartDate);
            onWayExceptionBo.setBeforeYesterdayEndDate(beforeYesterdayEndDate);
        }

        List<OrderOtd> orderOtdList = orderOtdService.exportExceptionData(customerNo, classCode, onWayExceptionBo);

        return orderOtdList;
    }
}
